package dhm.com.dhmshop.fragment.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.base.LazyLoadFragment;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.GetOneCategory;
import dhm.com.dhmshop.framework.module.home.activity.SearchActivity;
import dhm.com.dhmshop.framework.module.home.fragment.HomeHotFragment;
import dhm.com.dhmshop.framework.module.home.fragment.HomeTabTypeFragment;
import dhm.com.dhmshop.framework.utils.StringUtil;


public class HomeFragment extends LazyLoadFragment implements LoginContract.IView {

    @BindView(R.id.one)
    TextView one;
    @BindView(R.id.two)
    TextView two;
    @BindView(R.id.mainsearch_homefg)
    LinearLayout mainsearchHomefg;
    @BindView(R.id.tab_homefg)
    TabLayout homeTab;
    @BindView(R.id.vp_homefg)
    ViewPager homeVp;
    private List<Fragment> fragments;
    private List<GetOneCategory.CodeBean> tabEntities;
    private CategoryPageAdapter pageAdapter;
    private View view;
    private LinearLayout mMainsearchHomefg;
    private PressenterImpl pressenter;
    Unbinder unbinder;

    protected void initView() {
//        TabLayout homeTab = findViewById(R.id.tab_homefg);
//        ViewPager homeVp = findViewById(R.id.vp_homefg);
//        mMainsearchHomefg = findViewById(R.id.mainsearch_homefg);
        pressenter = new PressenterImpl();
        pressenter.attachView(this);
        fragments = new ArrayList<>();
        tabEntities = new ArrayList<>();
        pageAdapter = new CategoryPageAdapter(getChildFragmentManager());
        homeVp.setAdapter(pageAdapter);
        homeTab.setupWithViewPager(homeVp);

        Map<String, String> map = new HashMap<>();
        map.put("token", Constant.TOKEN);
        pressenter.sendMessage(getActivity(), Constant.GetOneCategory, map, GetOneCategory.class);
    }
/*
    @Override
    protected void initData() {
        //调接口发网络请求
        model.getTabCategory();
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }*/

  /*  @Override
    public void getCategorySuccess(List<TabCategoryEntity> result) {
        if (result == null) {
            return;
        }
        TabCategoryEntity tabCategoryEntity=new TabCategoryEntity();
        tabCategoryEntity.setName("热门");
        tabEntities.add(tabCategoryEntity);
        tabEntities.addAll(result);
        for (TabCategoryEntity categoryEntity : tabEntities) {
            if (categoryEntity.getName().equals("热门")) {
                fragments.add(new HomeHotFragment());
            } else {
                fragments.add(new HomeTabTypeFragment(categoryEntity.getId()));
            }
        }
        pageAdapter.notifyDataSetChanged();
    }*/


    private View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_home, container, false);
        }
        unbinder = ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    protected int setContentView() {
        return R.layout.fragment_home;
    }

    @Override
    protected void lazyLoad() {

    }

    private GetOneCategory getOneCategory;

    @Override
    public void requesta(Object data) {
        if (data instanceof GetOneCategory) {
            getOneCategory = (GetOneCategory) data;

            GetOneCategory.CodeBean tabCategoryEntity = new GetOneCategory.CodeBean();
            tabCategoryEntity.setName("热门");
            tabEntities.add(tabCategoryEntity);
            tabEntities.addAll(getOneCategory.getCode());

            for (int i=0;i<tabEntities.size();i++){
                if (tabEntities.get(i).getName().equals("热门")) {
                    fragments.add(new HomeHotFragment(this));
                } else {
                    fragments.add(new HomeTabTypeFragment(tabEntities.get(i).getId(),i));
                }
            }

            for (GetOneCategory.CodeBean categoryEntity : tabEntities) {

            }
            pageAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void fail(String error) {

    }


    public void initData(int position) {
        if (position==0){
            homeVp.setCurrentItem(8);
        }else if (position==1){
            homeVp.setCurrentItem(2);
        }else if (position==2){
            homeVp.setCurrentItem(5);
        }


    }

    @OnClick(R.id.mainsearch_homefg)
    public void onViewClicked() {
        Intent intent = new Intent(getActivity(), SearchActivity.class);
        intent.putExtra("type","3");
        startActivity(intent);
    }

    //内部类写viewPage适配器
    class CategoryPageAdapter extends FragmentPagerAdapter {

        public CategoryPageAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return fragments.get(i);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return StringUtil.preventNull(tabEntities.get(position).getName());
        }
    }
}
